package com.xinchao.controller;

import com.google.common.collect.Lists;
import com.xinchao.constant.FileConstant;
import com.xinchao.constant.ResponseConstant;
import com.xinchao.exception.ControllerException;
import com.xinchao.exception.ServiceException;
import com.xinchao.model.sys.JsonResponse;
import com.xinchao.service.FileService;
import org.apache.commons.io.FilenameUtils;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author dxy
 * @date 2018/9/28 17:24
 */
@Controller
@RequestMapping(value = "/file")
public class FileController {
	private static Logger logger = LoggerFactory.getLogger(FileController.class);
	@Autowired
	private FileService fileService;

	/**
	 * 上传视频
	 */
	@ResponseBody
	@RequestMapping(value = "/uploadVideo")
	public Object uploadVideo(@Param("files") MultipartFile files) {
		//获取文件全名
		String originalFilename = files.getOriginalFilename();
		//获取扩展名
		String extension = FilenameUtils.getExtension(originalFilename);
		if (!FileConstant.FILE_EXTENSION_AVI.equalsIgnoreCase(extension)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "视频格式不正确，只能上传avi格式的视频");
		}
		//返回列表
		List msgList = Lists.newArrayList();
		String cloudDir = FileConstant.CLOUD_DIR_VIDEO;
		try {
			Map<String, Object> fileInfoMap = fileService.uploadFile(files, cloudDir);
			msgList.add(fileInfoMap);
		} catch (IOException e) {
			e.printStackTrace();
			logger.error("FileController uploadVideo 上传视频失败：" + e.getMessage());
			return new ControllerException(e.getMessage());
		} catch (ServiceException e) {
			e.printStackTrace();
			logger.error("FileController uploadVideo 上传视频失败：" + e.getMessage());
			return new ControllerException(e.getMessage());
		}
		return msgList;
	}

	/**
	 * 上传视频
	 */
	@ResponseBody
	@RequestMapping(value = "/uploadImage")
	public Object uploadImage(@Param("files") MultipartFile files) {
		//获取文件全名
		String originalFilename = files.getOriginalFilename();
		//获取扩展名
		String extension = FilenameUtils.getExtension(originalFilename);
		if (!FileConstant.FILE_EXTENSION_JPG.equalsIgnoreCase(extension)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "图片格式不正确，只能上传jgp格式的图片");
		}
		//返回列表
		List msgList = Lists.newArrayList();
		String cloudDir = FileConstant.CLOUD_DIR_IMAGE;
		try {
			Map<String, Object> fileInfoMap = fileService.uploadFile(files, cloudDir);
			msgList.add(fileInfoMap);
		} catch (IOException e) {
			e.printStackTrace();
			logger.error("FileController uploadImage 上传图片失败：" + e.getMessage());
			return new ControllerException(e.getMessage());
		} catch (ServiceException e) {
			e.printStackTrace();
			logger.error("FileController uploadImage 上传图片失败：" + e.getMessage());
			return new ControllerException(e.getMessage());
		}
		return msgList;
	}
}
